/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSCamera.cpp
 *
 *  Created on: 2010/03/07
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSCamera.h"
#include "RSOptics.h"
#include "RSImager.h"
#include "RSRenderer.h"

namespace Realsphere {

    RSCamera::RSCamera ( const QString& rId, const QString& rName ) :
        RSUIDElement ( rId, RS_CAMERA_TAG )
    {
        SetName ( rName, false );
        mOptics = NULL;
        mImager = NULL;
    }

    RSCamera::~RSCamera ()
    {
        mOptics = NULL;
        mImager = NULL;
    }

    RSOptics* RSCamera::Optics () const
    {
        return mOptics;
    }

    RSImager* RSCamera::Imager () const
    {
        return mImager;
    }

    bool RSCamera::IsRootElement() const {
        return false;
    }

    RSError RSCamera::SetOptics ( RSOptics* rOptics )
    {
        RSError error = AddChildElement ( rOptics );
        return error;
    }

    RSError RSCamera::SetImager ( RSImager* rImager )
    {
        RSError error = AddChildElement ( rImager );
        return error;
    }

    QMatrix4x4 RSCamera::ProjectionMatrix () {

        if ( mOptics == NULL ) {
            QMatrix4x4 projection;
            return projection;
        }

        return mOptics->ProjectionMatrix();
    }

    RSError RSCamera::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {
            if ( rChild->InstanceOf ( RS_OPTICS_CLASS )
                    || rChild->InstanceOf ( RS_IMAGER_CLASS ) )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    bool RSCamera::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled )
        {
            if ( rChild->InstanceOf ( RS_OPTICS_CLASS ) )
            {
                if ( mOptics != NULL )
                    RemoveChildElement ( mOptics );

                mOptics = RSOpticsCast ( rChild );
                connect ( mOptics, SIGNAL ( ProjectionChanged() ), this, SIGNAL ( CameraChanged () ) );
                handled = true;

                emit OpticsAdded( mOptics );
            }
            else if ( rChild->InstanceOf ( RS_IMAGER_CLASS ) )
            {
                if ( mImager != NULL )
                    RemoveChildElement ( mImager );

                mImager = RSImagerCast ( rChild );
                handled = true;

                emit ImagerAdded ( mImager );
            }
        }

        return handled;
    }

    bool RSCamera::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled )
        {
            if ( rChild->InstanceOf ( RS_OPTICS_CLASS ) )
            {
                RSOptics* optics = RSOpticsCast ( rChild );
                disconnect ( mOptics, SIGNAL ( ProjectionChanged() ), this, SIGNAL ( CameraChanged () ) );
                mOptics = NULL;
                handled = true;

                emit OpticsRemoved ( optics );
            }
            else if ( rChild->InstanceOf ( RS_IMAGER_CLASS ) )
            {
                RSImager* imager = RSImagerCast ( rChild );
                mImager = NULL;
                handled = true;

                emit ImagerRemoved ( imager );
            }
        }

        return handled;
    }
}
